<!--
 	* @author  wangjinqiu
-->
<template>
  <div id="hundredLearningCourse">
    <el-collapse-transition>
      <div v-show="searchshow" class="common-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model="listQuery.name"
          clearable
          placeholder="按课程名称进行搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <div class="common-search-btn">
          <el-button
            class="filter-item"
            type="primary"
            @click="handleFilter"
          >{{ $t('table.search') }}</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="common-search-more" @click="searchshow = !searchshow">
            更多筛选条件
            <i class="el-icon-arrow-down" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <el-collapse-transition>
      <div v-show="!searchshow" class="common-search separate">
        <div class="common-search-inner">
          <el-row :gutter="20">
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">课程名称</div>
                <el-input
                  v-model="listQuery.name"
                  clearable
                  class="filter-item"
                  style="width: 100%;"
                  placeholder="按课程名称进行搜索"
                />
              </div>
            </el-col>
            <el-col :span="8">
              <div class="search-inner-item">
                <div class="item-label">状态</div>
                <el-select v-model="search.status" clearable style="width: 90%;" placeholder="请选择">
                  <el-option label="请选择" value />
                  <el-option :value="1" label="停用" />
                  <el-option :value="0" label="发布" />
                </el-select>
              </div>
            </el-col>

          </el-row>
        </div>
        <div class="common-search-btn">
          <el-button type="primary" @click="handleFilter">{{ $t('table.search') }}</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
          <span class="common-search-more" @click="searchshow = !searchshow">
            精简筛选条件
            <i class="el-icon-arrow-up" />
          </span>
        </div>
      </div>
    </el-collapse-transition>
    <div class="btn-container">
      <div class="btn-title">
        <span>课程列表</span>
      </div>
      <div class="btn-btns">
        <el-button v-waves style="margin-left: 10px;" @click="handleCreate">新建</el-button>
        <el-button v-waves style="margin-left: 10px;" @click="handleDeletes">批量删除</el-button>
      </div>
    </div>
    <div class="table">
      <v-table
        :list="list"
        tag="dkhdcourselist"
        :list-loading="listLoading"
        :filter-column="filtercolumn"
        :selection="false"
        @handleSelectionChange="handleSelectionChange"
      >
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleUpdateState(scope.row)"
            >{{ scope.row.status=='0'?'停用': '发布' }}</el-button>
            <el-button v-if="scope.row.status != '0'" type="text" size="mini" @click="handleSelect(scope.row)">选择课件</el-button>
            <el-button type="text" size="mini" @click="handleInfo(scope.row)">查看</el-button>
            <el-button v-if="scope.row.status != '0'" type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button v-if="scope.row.status != '0'" type="text" size="mini" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </v-table>
      <!-- <el-table
        ref="rolemantable"
        v-loading="listLoading"
        :data="list"
        class="tableScope"
        style="width: 100%"
        :header-cell-style="rowClass"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column prop="imageId" label="课程封面" width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <img :src="view + scope.row.imageId" max-width="70" height="70">
          </template>
        </el-table-column>
        <el-table-column prop="name" label="课程名称" width="200" show-overflow-tooltip />
        <el-table-column prop="remark" label="课程简介" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-html="scope.row.remark" />
          </template>
        </el-table-column>
        <el-table-column prop="videoNum" label="课件数量" width="200" show-overflow-tooltip />
        <el-table-column prop="status" label="状态" width="80" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ scope.row.status=='0'?'发布': '停用' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          fixed="right"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleUpdateState(scope.row)"
            >{{ scope.row.status=='0'?'停用': '发布' }}</el-button>
            <el-button v-if="scope.row.status != '0'" type="text" size="mini" @click="handleSelect(scope.row)">选择课件</el-button>
            <el-button type="text" size="mini" @click="handleInfo(scope.row)">查看</el-button>
            <el-button v-if="scope.row.status != '0'" type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button v-if="scope.row.status != '0'" type="text" size="mini" @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table> -->
    </div>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
// import { courseQueryList, courseDel, courseChangeState } from '@/api/hundredLearning/index'
import waves from '@/directive/waves' // Waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import VTable from '@/components/Table'
export default {
  name: 'PunchClockCourse',
  components: { Pagination, VTable },
  directives: { waves },
  data() {
    return {
      view: '/API-RESOURCE/webView?fid=',
      searchshow: true,
      search: {
        status: 1,
        time: ''
      },
      listQuery: {
        page: 1,
        limit: 10,
        name: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      multipleSelection: [],
      pickerOptions: {
        // disabledDate(time) {
        //   return time.getTime() < Date.now() - 8.64e7
        // }
      }
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    filtercolumn(scope, item) {
      if (item === 'imageId') {
        return ` <img src="${this.view} + ${scope.row[item]}" max-width="70" height="70">`
      }
      if (item === 'remark') {
        return scope.row[item]
      }
      if (item === 'status') {
        return scope.row[item] == '0' ? '发布' : '停用'
      }
      return scope.row[item]
    },
    handleSelectionChange(arr) {
      this.multipleSelection = arr
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resettingBtn() {
      this.listQuery.name = ''
      for (const i in this.search) {
        this.search['' + i + ''] = ''
      }
      this.listQuery.page = 1
      this.getList()
    },
    handleUpdateState(row) {
      const status = row.status == 1 ? 0 : 1
      courseChangeState({ id: row.id, status: status }).then(() => {
        this.$notify({
          title: '成功',
          message: '修改状态成功',
          type: 'success',
          duration: 2000
        })
        this.getList()
      })
    },
    handleCreate() {
      this.$router.push({ path: this.$filterPath('courseEdit'), query: { type: 'c' }})
    },
    handleUpdate(item) {
      this.$router.push({ path: this.$filterPath('courseEdit'), query: { type: 'u', id: item.id }})
    },
    handleSelect(item) {
      this.$router.push({ path: this.$filterPath('courseware'), query: { type: 'c', courseId: item.id }})
    },
    handleInfo(item) {
      this.$router.push({ path: this.$filterPath('courseware'), query: { type: 'u', status: item.status, courseId: item.id }})
    },
    handleDel(row) {
      this.deleteItem(row.id)
    },
    handleDeletes() {
      const arr = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach(item => {
          arr.push(item.id)
        })
        var ids = arr.join(',')
        this.deleteItem(ids)
      } else {
        this.$message({
          message: '请选择需要删除的数据！',
          type: 'warning'
        })
      }
    },
    deleteItem(ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return new Promise((resolve, reject) => {
          courseDel({ ids: ids }).then(() => {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).catch(error => {
            reject(error)
          })
        })
      }).catch((e) => {
        // this.$message({
        //   type: 'info',
        //   message: e.msg||'已取消删除'
        // })
      })
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        name: this.listQuery.name
      }
      if (!this.searchshow) {
        query['seasonYear'] = this.search.seasonYear
        query['status'] = this.search.status
        query['beginTime'] = this.search.time && this.search.time.length > 0 ? this.search.time[0] : ''
        query['endTime'] = this.search.time && this.search.time.length > 0 ? this.search.time[1] : ''
      }
      return new Promise((resolve, reject) => {
        courseQueryList(query).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    }
  }
}
</script>

<style lang="scss">
$dark_gray: #bfced6;
$mainColor: #18a5fd;
#hundredLearningCourse {
  padding: 20px;
  .common-search {
    background-color: #fff;
    padding: 0 20px;
    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .common-search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .common-search-more {
        font-size: 14px;
        font-weight: 500;
        color: $mainColor;
        cursor: pointer;
        margin-left: 10px;
      }
    }
    &.merge {
      height: 50px;
      line-height: 50px;
    }
    &.separate {
      .common-search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor .el-input__inner {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: "";
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .common-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .btn-container {
    display: flex;
    height: 36px;
    line-height: 36px;
    margin-top: 20px;
    margin-bottom: 20px;
    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      font-family: PingFangSC-Medium;
      color: rgba(51, 51, 51, 1);
    }
  }
  .dataForm::-webkit-scrollbar {
    display: none;
  }
  .avatar-uploader {
    cursor: pointer;
    position: relative;
    overflow: hidden;
    background: rgba(247, 247, 248, 1);
    border: 1px solid rgba(226, 227, 232, 1);
    border-radius: 4px;
    width: 300px;
    height: 180px;
    &:hover {
      border-color: #fe5a61;
    }
    img {
      width: 100%;
      height: 100%;
    }
    .avatar-uploader-icon {
      font-size: 48px;
      color: #dadada;
      width: 300px;
      height: 180px;
      line-height: 180px;
      text-align: center;
    }
  }
}
</style>
